import { createRouter, createWebHashHistory } from 'vue-router'
import { useUserStore, useGlobalStore } from '@/stores'

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    { path: '/login', component: () => import('@/views/login/LoginPage.vue') },
    {
      path: '/',
      component: () => import('@/views/layout/LayoutContainer.vue'),
      redirect: '/buckets',
      children: [
        {
          path: '/buckets',
          component: () => import('@/views/buckets/BucketsContainer.vue'),
        },
        {
          path: '/buckets/create',
          component: () => import('@/views/buckets/BucketsCreate.vue'),
        },
        {
          path: '/buckets/show',
          component: () => import('@/views/buckets/BucketsShow.vue'),
          children: [
            {
              path: '/buckets/show/summary',
              component: () => import('@/views/buckets/show/BucketsShowSummary.vue')
            }
          ]
        },
        {
          path: '/buckets/objects',
          component: () => import('@/views/buckets/BucketsShowObjects.vue'),
        },
        {
          path: '/identity',
          component: () => import('@/views/identity/IdentityContainer.vue'),
        },
        {
          path: '/identity/create',
          component: () => import('@/views/identity/IdentityCreate.vue'),
        },
        {
          path: '/identity/show',
          component: () => import('@/views/identity/IdentityShow.vue'),
        },
        {
          path: '/events',
          component: () => import('@/views/events/EventsContainer.vue'),
        },
        {
          path: '/events/create',
          component: () => import('@/views/events/EventsCreate.vue'),
        },
        {
          path: '/events/create/mysql',
          component: () => import('@/views/events/notify/EventsMysql.vue'),
        },
        {
          path: '/events/create/redis',
          component: () => import('@/views/events/notify/EventsRedis.vue'),
        },
        {
          path: '/events/create/webhook',
          component: () => import('@/views/events/notify/EventsWebhook.vue'),
        },
        {
          path: '/policies',
          component: () => import('@/views/policies/PoliciesContainer.vue')
        },
        {
          path: '/policies/create',
          component: () => import('@/views/policies/PoliciesCreate.vue')
        },
        {
          path: '/policies/show',
          component: () => import('@/views/policies/PoliciesShow.vue')
        },
      ],
    },
  ],
})

router.beforeEach(async (to) => {
  // 如果没有token, 且访问的是非登录页
  const userStore = useUserStore()
  if (!userStore.token && to.path !== '/login') {
    return '/login'
  }
  const matches = to.path.match(/\/+/g)
  const globalStore = useGlobalStore()
  if (matches && matches.length >= 2) {
    globalStore.setIsShowLeftArrow(true)
  } else {
    globalStore.setIsShowLeftArrow(false)
  }
  return true
})

export default router
